package ws.jdbc.concurrent.driver.sqlserver.tds

import ws.utils.ArrayUtil
import static ws.jdbc.concurrent.driver.sqlserver.tds.TDSMessage.*

/**
 * This test sends the examples found on the TDS documentation.
 *
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
class TDSMessageExamples extends GroovyTestCase {

  private String serverName = "goldberg-wm"
  private String userName = "sa"
  private String password = "sg4765j"
  private int portNumber = 1433

  public void setUp() {
    println "-----------------------------------------------------------------"
  }

  public void testSendPreLoginExample() {
    println "testSendPreLoginExample()"
    def data = [
      // <editor-fold defaultstate="collapsed" desc="Pre-Login data">
      0x00, 0x00, 0x15, 0x00, 0x06, 0x01, 0x00, 0x1b, 0x00, 0x01, 0x02, 0x00,
      0x1c, 0x00, 0x0c, 0x03, 0x00, 0x28, 0x00, 0x04, 0xff, 0x08, 0x00,
      0x01, 0x55, 0x00, 0x00, 0x00, 0x4d, 0x53, 0x53, 0x51, 0x4c, 0x53, 0x65,
      0x72, 0x76, 0x65, 0x72, 0x00, 0x80, 0x19, 0x00, 0x00
      // </editor-fold>
    ] as Byte[];
    this.send(TYPE_PRE_LOGIN, data);
  }

	public void testSendLogin7Example() {
    println "testSendLogin7Example()"

    def data = [
      // <editor-fold defaultstate="collapsed" desc="Login7 data">
      0x88, 0x00, 0x00, 0x00, 0x02, 0x00, 0x09, 0x72,
      0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0xE0, 0x03, 0x00, 0x00, 0xE0, 0x01, 0x00, 0x00, 0x09, 0x04, 0x00, 0x00, 0x5E, 0x00, 0x08, 0x00,
      0x6E, 0x00, 0x02, 0x00, 0x72, 0x00, 0x00, 0x00, 0x72, 0x00, 0x07, 0x00, 0x80, 0x00, 0x00, 0x00,
      0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x04, 0x00, 0x88, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00,
      0x00, 0x50, 0x8B, 0xE2, 0xB7, 0x8F, 0x88, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x88, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73, 0x00, 0x6B, 0x00, 0x6F, 0x00, 0x73, 0x00, 0x74, 0x00,
      0x6F, 0x00, 0x76, 0x00, 0x31, 0x00, 0x73, 0x00, 0x61, 0x00, 0x4F, 0x00, 0x53, 0x00, 0x51, 0x00,
      0x4C, 0x00, 0x2D, 0x00, 0x33, 0x00, 0x32, 0x00, 0x4F, 0x00, 0x44, 0x00, 0x42, 0x00, 0x43, 0x00,
      // </editor-fold>
    ] as Byte[];
    
    this.send(TYPE_LOGIN7, data);
  }

  public void testSendSQLBatchRequest() {
    println "testSendSQLBatchRequest()"
    
    TDSMessage m = new TDSMessage(new Socket(serverName, portNumber))
    m.login("", userName, password, serverName)

    def data = [
      // All headers
      0x16, 0x00, 0x00, 0x00, // Total length
      0x12, 0x00, 0x00, 0x00, // Header length
      0x02, 0x00,             // Header type
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // TransactionDescriptor
      0x00, 0x00, 0x00, 0x01, // OutstandingRequestCount
      // Query - Select 'foo' as 'bar'
      0x0A, 0x00, 0x73, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x65, 0x00, 0x63, 0x00, 0x74,
      0x00, 0x20, 0x00, 0x27, 0x00, 0x66, 0x00, 0x6F, 0x00, 0x6F, 0x00, 0x27, 0x00,
      0x20, 0x00, 0x61, 0x00, 0x73, 0x00, 0x20, 0x00, 0x27, 0x00, 0x62, 0x00, 0x61,
      0x00, 0x72, 0x00, 0x27, 0x00, 0x0A, 0x00, 0x20, 0x00, 0x20, 0x00, 0x20, 0x00,
      0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x20, 0x00,
    ] as Byte[];

    m.send(TYPE_SQL_BATCH, data)
  }
  
  private void send(byte type, byte[] data) {
    TDSMessage m = new TDSMessage(new Socket(serverName, portNumber))
    byte[] input = new byte[512]

    m.send(type, data);

    def count = m.readResponseData(input)
    ArrayUtil.printHex(input, count)
    assertEquals TYPE_TABLE_RESPONSE, input[0]
  }
}

